package com.gopay.cashier.service.impl;

import com.gopay.cashier.dao.LogSMSSendDAO;
import com.gopay.cashier.service.SmsSupportService;
import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.constants.cust.SMSType;
import com.gopay.common.domain.log.LogSMSSend;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.StringMaskUtil;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.constants.SMSResCode;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 14-8-21.
 */
@Service("smsSupportService")
public class SmsSupportServiceImpl implements SmsSupportService {

    @Resource(name="logSMSSendDAO")
    private LogSMSSendDAO logSMSSendDAO;

    private static Logger log = Logger.getLogger("SMS-SERVICE");
    public boolean sendSMS(String mobileNo,String content,boolean record){
//        log.info("send sms mobileNo:"+mobileNo+";content:"+content);
        if(StringUtils.isBlank(mobileNo) || StringUtils.isBlank(content)){
            return false;
        }
        RequestVO req = new RequestVO();
        req.setMobile(mobileNo);
        req.setAppId(SMSAppId._90001.value);
        req.setContent(content);
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().createVerificationCode(req);
        boolean res =  resposeVo!=null && resposeVo.getStatusCode().equals(SMSResCode.SMS_DEND_SUCCESS.value);
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("send sms mobileNo:"+LoggerMaskUtils.maskString(mobileNo)+";result:"+res);
        /** end */
       
        if(res && record)//发送成功，记录发送成功次数
            buildLog(mobileNo,SMSType.CASHIER_SMS_SEND);
        return res;
    }

    @Override
    public boolean sendLoginSMS(String mobileNo, String content) {
        if(StringUtils.isBlank(mobileNo) || StringUtils.isBlank(content)){
            return false;
        }
        RequestVO req = new RequestVO();
        req.setMobile(mobileNo);
        req.setAppId("20000");//表示登录
        req.setContent(content);
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().createVerificationCode(req);
        boolean res =  resposeVo!=null && resposeVo.getStatusCode().equals(SMSResCode.SMS_DEND_SUCCESS.value);
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("send sms mobileNo:"+LoggerMaskUtils.maskString(mobileNo)+";result:"+res);
        /** end */
        return res;
    }

    public boolean checkSMS(String mobileNo, String mobileCode) {
        RequestVO req = new RequestVO();
        req.setAppId(SMSAppId._90001.value);
        req.setMobile(mobileNo);
        req.setCode(mobileCode);
        req.setTimeout("90");//验证码90失效
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().verifyCode(req);
        boolean res =  resposeVo !=null && StringUtils.equals(resposeVo.getStatusCode(), SMSResCode.SMS_VERIFY_SUCCESS.value);
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("check sms mobileNo:"+LoggerMaskUtils.maskString(mobileNo)+";result:"+res);
        /** end */
       
      
        if(res){
            //验证正确，错误次数清零
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("mobileNum",mobileNo);
            param.put("mobileID",SMSType.CASHIER_SMS_ERROR.value);
            param.put("sendTime", DateUtils.format(new Date(), "yyyy-MM-dd"));
            logSMSSendDAO.updateByHql("DELETE FROM LogSMSSend L WHERE L.mobileNum =:mobileNum AND L.mobileID =:mobileID AND L.sendTime >= to_timestamp(:sendTime , 'yyyy-MM-dd')",param);
        }else{
            //验证错误，记录错误次数
            buildLog(mobileNo,SMSType.CASHIER_SMS_ERROR);
        }
        return res;
    }

    public boolean checkSendSmsCount(String mobileNo,int maxCount){
        int count = logSMSSendDAO.findToDayCountByMobileNum(mobileNo, SMSType.CASHIER_SMS_SEND.value);
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("check sms send count:"+LoggerMaskUtils.maskString(mobileNo)+";count:"+count);
        /** end */
        return !(count > maxCount-1);
    }

    public int checkFailSmsCount(String mobileNo){
        int count = logSMSSendDAO.findToDayCountByMobileNum(mobileNo, SMSType.CASHIER_SMS_ERROR.value);
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("check sms fail count:"+LoggerMaskUtils.maskString(mobileNo)+";count:"+count);
        /** end */
        return count;
    }
    @Async
    private void buildLog(String mobileNo,SMSType smsType){
        LogSMSSend log = new LogSMSSend();
        Date date = new Date();
        log.setMobileNum(mobileNo);
        log.setMobileID(smsType.value);
        log.setSendTime(date);
        log.setLastUpdTs(date);
        log.setCreatedTs(date);
        logSMSSendDAO.save(log);
    }
    @Override
    public boolean sendMTSMS(String mobileNo, String content) {
        if(StringUtils.isBlank(mobileNo) || StringUtils.isBlank(content)){
            return false;
        }
        RequestVO req = new RequestVO();
        req.setMobile(mobileNo);
        req.setAppId(SMSAppId._90000.value);
        req.setContent(content);
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().createVerificationCode(req);
        boolean res =  resposeVo!=null && resposeVo.getStatusCode().equals(SMSResCode.SMS_DEND_SUCCESS.value);
        
        /**start ---手机号日志掩码处理 ssj 2017-04-18*/
        log.info("send sms mobileNo:"+LoggerMaskUtils.maskString(mobileNo)+";result:"+res);
        /** end */
     
        return res;
    }
}
